//
//  AIGirlGuidanceGiftView.swift
//  AIGirl
//
//  Created by Rae on 2024/1/2.
//

import UIKit
import SwiftEntryKit

class AIGirlGuidanceGiftView: UIView, AnimationProtocol, AnimationDelegate {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth - 30.0, height: 355.0)
    }
    
    func animationContainerViewStyle() -> AnimationViewStyle {
        .center
    }
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Message_Guidance_gift").mode(.scaleAspectFill).end()
    private var diamondLable = UIIconButton(style: .leading(itemSpace: 6.5))
    private var desLabel = c_make_ex(UILabel.self)
        .color("#E2D2FC".toColor.alpha(0.84))
        .text("Choose a small gift and reward the\ngirl! She’ll be super happy!")
        .font(16.font_bold).line(2).alignment(.center).end()
    private lazy var sendButton = c_make_ex(AIGirlSubmitButton.self)
        .color(.white).font(21.font_bold).corner(20)
        .text("Send").end()
    
    var giftModel: AIGirlGiftItemModel?{
        didSet{
            guard let md = giftModel else { return }
            iconImageView.loadImage(url: md.preUrl)
            diamondLable.setTitle(md.price, for: .normal)
        }
    }
    
    var sendGiftClosure: ((_ model: AIGirlGiftItemModel) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        layer.cornerRadius = 30
        backgroundColor = "#1B1325".toColor
        
        subviews{
            iconImageView
            diamondLable
            desLabel
            sendButton
        }
        
        iconImageView.top(15).size(150.0).centerHorizontally()
        diamondLable.top(173.0).centerHorizontally()
        desLabel.centerHorizontally()
        desLabel.Top == diamondLable.Bottom + 10
        sendButton.leading(22.5).trailing(22.5).height(50).bottom(20)
        
        diamondLable.style { v in
            v.setImage("AIGirl_Home_purpleDiamond".toImage, for: .normal)
            v.setTitleColor(.white, for: .normal)
            v.titleLabel?.font = 24.font_semiBold
            v.iconSize = CGSize(width: 37, height: 37)
            v.setTitle("20", for: .normal)
            v.contentHorizontalAlignment = .center
            v.isUserInteractionEnabled = false
        }
        
        sendButton.addTarget(self, action: #selector(clickSendButton), for: .touchUpInside)
    }
    
    @objc private func clickSendButton(){
        guard let md = giftModel else { return }
        
        animation.animation(dismiss: self){
            if Int(md.price) ?? 0 <= GlobalTool.userModel?.money ?? 0 {
                self.sendGiftClosure?(md)
            }else{
                "Diamonds not enough，please recharge!".toast_error
                self.animation.animation(show: AIGirlMessagePremiumView())
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
